package com.ibm.eg.cvmanager.business.layer;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.transaction.RollbackException;

import com.ibm.eg.cvmanager.business.interfaces.InterviewBusiness;
import com.ibm.eg.cvmanager.dao.impl.ApplicantDao;
import com.ibm.eg.cvmanager.dao.impl.InterviewDao;
import com.ibm.eg.cvmanager.dto.Applicant;
import com.ibm.eg.cvmanager.dto.INTERVIEW_STATUS;
import com.ibm.eg.cvmanager.dto.Interview;

import exception.BusinessException;
import exception.TechnicalException;


public class InterviewBusinessImpl implements InterviewBusiness
{
	public void addInterview(Interview interview)
	{
		InterviewDao x = new InterviewDao();
		try{
			x.beginTransaction();
			x.add(interview);
			x.commitTransaction();
		}
		catch(Exception re)
		{
			x.rollbackTransaction();
		}
	}
	public void updateInterview(Interview interview)
	{
		InterviewDao x = new InterviewDao();
		try{
			
			x.beginTransaction();
			x.update(interview);
			x.commitTransaction();
		}
		catch(Exception re)
		{
			x.rollbackTransaction();
		}
		
	}
	public void deleteInterview(int id)
	{
		Interview in = new Interview();
		in.setId(id);
		InterviewDao x = new InterviewDao();
		try{
			x.beginTransaction();
			x.delete(in);
			x.commitTransaction();
		}
		catch(Exception re)
		{
			x.rollbackTransaction();
		}
	}
	
	public ArrayList<Interview> getAll() throws TechnicalException
	{
		
		InterviewDao x =new InterviewDao();
		return x.getAll(Interview.class);
	}
	public ArrayList<Interview> filterInterviews(Date startDate, Date endDate, INTERVIEW_STATUS status) throws TechnicalException, BusinessException
	{
		InterviewDao x =new InterviewDao();
		if (startDate.before(Calendar.getInstance().getTime()))
			throw new BusinessException("Invalid date: cannot choose date before today's");
		return x.filterInterviews(startDate, endDate, status.ordinal());
	}
	public ArrayList<Interview> filterInterviews(Date startDate, Date endDate) throws TechnicalException, BusinessException
	{
		InterviewDao x =new InterviewDao();
		if (startDate.before(Calendar.getInstance().getTime()))
			throw new BusinessException("Invalid date: cannot choose date before today's");
		return x.filterInterviews(startDate, endDate);
	}
	public void markAsFailed(int interviewId) throws TechnicalException
	{
		InterviewDao x =new InterviewDao();
		x.markAsFailed(interviewId)	;
	}
	public void markAsSucceeded(int interviewId) throws TechnicalException
	{
		InterviewDao x=new InterviewDao();
		x.markAsSucceeded(interviewId);
	}
	public Interview getInterview(int id) throws TechnicalException
	{
		InterviewDao x = new InterviewDao();
		return x.getInterview(id);
	}
}
